package app.view 
{
	import app.data.RSSItemVO;

	import com.bumpslide.events.UIEvent;
	import com.bumpslide.tween.FTween;
	import com.bumpslide.ui.Grid;
	import com.bumpslide.ui.Panel;
	import com.bumpslide.ui.TextPanel;
	import com.bumpslide.ui.constants.AspectRatio;
	import com.bumpslide.util.Link;
	import com.bumpslide.util.LoremIpsum;

	import flash.display.BlendMode;

	/**
	 * All the main section content is setup here
	 * 
	 * @author David Knape
	 */
	public class SectionContent extends AppComponent 
	{

		public var intro:TextPanel;

		public var posts:Grid;

		public var puppies:Grid;

		
		override protected function addChildren():void 
		{
			super.addChildren();
			
			Panel.DEFAULT_BACKGROUND_COLOR = 0x333333;
			Panel.DEFAULT_PADDING = 10;
			
			intro = new TextPanel();
			intro.background.alpha = .8;
			intro.scrollbar.blendMode = BlendMode.MULTIPLY;
			intro.text = LoremIpsum.TEXT;
			addChild(intro);
			
			posts = new Grid(null);
			posts.background.alpha = .8;
			posts.scrollbar.blendMode = BlendMode.MULTIPLY;
			posts.dataProvider = LoremIpsum.BIRDS;
			posts.spacing = 1;
			addChild(posts);
			
			puppies = new Grid();
			puppies.background.alpha = .8;
			puppies.spacing = 10;
			puppies.scrollbar.blendMode = BlendMode.MULTIPLY;
			puppies.gridItemRenderer = CustomThumbnailItem;
			puppies.fixedAspectRatio = AspectRatio.GOLDEN;
			puppies.dataProvider = LoremIpsum.PUPPY_IMAGES;						
			addChild(puppies);
			
			intro.visible = false;
			posts.visible = false;
			puppies.visible = false;
			
			model.bind('blogPosts', posts, 'dataProvider');
			model.bind('currentSection', this, 'section');
			
			// load blog posts if we don't have them
			if(model.blogPosts == null) service.loadBlogPosts();
			
			posts.addEventListener(Grid.EVENT_ITEM_CLICK, handleBlogPostClick);			puppies.addEventListener(Grid.EVENT_ITEM_CLICK, handleImageClick);
		}

		
		private function handleImageClick(event:UIEvent):void 
		{
			model.selectedImageUrl = event.data as String;
		}

		
		private function handleBlogPostClick(event:UIEvent):void 
		{
			Link.open(RSSItemVO(event.data).link);
		}

		
		public function set section(n:int):void {
			
			FTween.fadeOut(intro);			FTween.fadeOut(posts);			FTween.fadeOut(puppies);
						
			// tell grids to sleep
			posts.layout.sleep();
			puppies.layout.sleep();
			
			switch( n ) {
				case 0: 
					FTween.fadeIn(intro, 500); 
					break;				case 1: 
					FTween.fadeIn(posts, 500); 
					posts.layout.wake(); 
					break;				case 2: 
					FTween.fadeIn(puppies, 500); 
					puppies.layout.wake(); 
					break;
			}
			
			invalidate();
		}

		
		override protected function draw():void 
		{
			super.draw();
						
			intro.setSize(width, height);
			
			posts.columnWidth = width;
			posts.rowHeight = 20;
			posts.setSize(width, height);
			
			puppies.fixedColumnCount = width / 200;
			puppies.setSize(width, height);
		}
	}
}

import com.bumpslide.ui.ThumbnailItem;


class CustomThumbnailItem extends ThumbnailItem 
{

	override protected function addChildren():void 
	{
		super.addChildren();
		padding = 0;
		fillColor = 0x666666;
		borderThickness = 1;
		borderColor = 0x333333;
	}	
}
